package com.my.config.ds.datarouting;

import com.alibaba.druid.pool.DruidDataSource;
import com.my.common.BaseDao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wt on 2018/2/8.
 */
@Configuration
public class DataSourceConfig {

    @Autowired
    private DruidDBProperties druidDbProperties;
    @Autowired
    private DBProperties dbProperties;
    @Autowired
    private MybatisProperties mybatisProperties;

//    @Bean(name = "druidDataSource")
//    public DruidDataSource druidDataSource() throws SQLException {
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setDriverClassName(druidDataSource.getDriverClassName());
////        druidDataSource.setUrl(url);
////        druidDataSource.setUsername(user);
////        druidDataSource.setPassword(password);
//        druidDataSource.setInitialSize(Integer.parseInt(druidDbProperties.getInitialSize()));
//        druidDataSource.setMinIdle(Integer.parseInt(druidDbProperties.getMinIdle()));
//        druidDataSource.setMaxActive(Integer.parseInt(druidDbProperties.getMaxActive()));
//        druidDataSource.setMaxWait(Long.parseLong(druidDbProperties.getMaxWait()));
//        druidDataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(druidDbProperties.getTimeBetweenEvictionRunsMillis()));
//        druidDataSource.setMinEvictableIdleTimeMillis(Long.parseLong(druidDbProperties.getMinEvictableIdleTimeMillis()));
//        druidDataSource.setValidationQuery(druidDbProperties.getValidationQuery());
//        druidDataSource.setTestWhileIdle(Boolean.parseBoolean(druidDbProperties.getTestWhileIdle()));
//        druidDataSource.setTestOnBorrow(Boolean.parseBoolean(druidDbProperties.getTestOnBorrow()));
//        druidDataSource.setTestOnReturn(Boolean.parseBoolean(druidDbProperties.getTestOnReturn()));
//        druidDataSource.setPoolPreparedStatements(Boolean.parseBoolean(druidDbProperties.getPoolPreparedStatements()));
//        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(druidDbProperties.getMaxPoolPreparedStatementPerConnectionSize()));
//        druidDataSource.setFilters(druidDbProperties.getFilters());
//        return druidDataSource;
//    }
//
//    @Bean(name = "hxjbAppDS")
//    public DruidDataSource appDataSource() throws SQLException {
//        DruidDataSource druidDataSource = druidDataSource();
//
//        druidDataSource.setUrl(dbProperties.getAppUrl());
//        druidDataSource.setUsername(dbProperties.getAppUsername());
//        druidDataSource.setPassword(dbProperties.getAppPassword());
//
//        return druidDataSource;
//    }
//
//    @Bean(name = "hxjbLogsmsDS")
//    public DruidDataSource logsmsDataSource() throws SQLException {
//        DruidDataSource druidDataSource = druidDataSource();
//
//        druidDataSource.setUrl(dbProperties.getLogsmsUrl());
//        druidDataSource.setUsername(dbProperties.getLogsmsUsername());
//        druidDataSource.setPassword(dbProperties.getLogsmsPassword());
//
//        return druidDataSource;
//    }
//
//
//    @Bean(name = "hxjbAnalysisDS")
//    public DruidDataSource analysisDataSource() throws SQLException {
//        DruidDataSource druidDataSource = druidDataSource();
//
//        druidDataSource.setUrl(dbProperties.getAnalysisUrl());
//        druidDataSource.setUsername(dbProperties.getAnalysisUsername());
//        druidDataSource.setPassword(dbProperties.getAnalysisPassword());
//
//        return druidDataSource;
//    }
//
//    @Bean
//    public DynamicDataSource dynamicDataSource() throws SQLException {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put(DataSourceKeyEnum.HXJB_USER, appDataSource());
//        targetDataSources.put(DataSourceKeyEnum.HXJB_LOG_SMS, logsmsDataSource());
//        targetDataSources.put(DataSourceKeyEnum.HXJB_ANALYSIS, analysisDataSource());
//
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        dynamicDataSource.setDefaultTargetDataSource(appDataSource());
//
//        return dynamicDataSource;
//    }
//
//    //装配mybatis
//    @Bean
//    public SqlSessionFactoryBean sqlSessionFactoryBean() throws SQLException {
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//
//        sqlSessionFactoryBean.setDataSource(dynamicDataSource());
//        sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(mybatisProperties.getConfigLocation()));
//
//        return sqlSessionFactoryBean;
//    }
//
//    @Bean
//    public BaseDao baseDao() throws SQLException {
//        return new BaseDao((SqlSessionFactory) sqlSessionFactoryBean());
//    }
//
//    @Bean
//    public DataSourceExchangeAspect dataSourceExchangeAspect() {
//        return new DataSourceExchangeAspect();
//    }
//
//    //声明式事务
//    @Bean
//    public DataSourceTransactionManager dataSourceTransactionManager() throws SQLException {
//        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
//        dataSourceTransactionManager.setDataSource(dynamicDataSource());
//
//        return dataSourceTransactionManager;
//    }




}
